/*
let App = getApp(),
  wxParse = require("../../wxParse/wxParse.js");
*/
const Util = require("../../utils/util");
const {addressList, getMessageInfo} = require("../../api/member");

const app = getApp()
Page({

  data: {
    nav_select: false, // 快捷导航

    indicatorDots: false, // 是否显示面板指示点
    autoplay: true, // 是否自动切换
    interval: 3000, // 自动切换时间间隔
    duration: 800, // 滑动动画时长

    currentIndex: 1, // 轮播图指针
    floorstatus: false, // 返回顶部
    showView: true, // 显示商品规格

    detail: {}, // 商品详情信息
    goods_price: 0, // 商品价格
    line_price: 0, // 划线价格
    goods_sku_title: '',
    stock_num: 0, // 库存数量

    goods_num: 1, // 商品数量
    goods_sku_id: 0, // 规格id
    cart_total_num: 0, // 购物车商品总数量
    specData: {}, // 多规格信息
    showModalStatus: false,
    animationData: '',
    is_collect: 0,
  },
  // 记录规格的数组
  goods_spec_arr: [],

  onLoad: function (options) {
    let _this = this;
    // 商品id
    _this.data.goods_id = options.goods_id;

    // 获取商品信息
    // _this.getGoodsDetail();
  },

  /**
   * 获取商品信息
   */
  getGoodsDetail: function () {
    let _this = this;
    App._get('goods/detail', {
      goods_id: _this.data.goods_id
    }, function (result) {
      // 初始化商品详情数据
      let data = _this.initGoodsDetailData(result.data);
      _this.setData(data);
    });
  },

  /**
   * 初始化商品详情数据
   */
  initGoodsDetailData: function (data) {
    let _this = this;

    // 富文本转码
    if (data.detail.content.length > 0) {
      wxParse.wxParse('content', 'html', data.detail.content, _this, 0);
    }

    let spec_item = data.standards.spec_attr[0].spec_items;
    if (spec_item) {
      data.goods_sku_id = spec_item.length ? spec_item[0].id : 0;
      data.goods_sku_title = spec_item[0].title;
    }

    // 商品价格/划线价/库存
    data.goods_price = data.detail.price;
    data.line_price = data.detail.line_price;
    data.stock_num = data.detail.stock_num;
    // // 初始化商品多规格
    if (data.detail.spec_type === 20) {
      data.specData = _this.initManySpecData(data.standards);
    }

    return data;
  },

  /**
   * 初始化商品多规格
   */
  initManySpecData: function (data) {
    for (let i in data.spec_attr) {
      for (let j in data.spec_attr[i].spec_items) {
        if (j < 1) {
          data.spec_attr[i].spec_items[0].checked = true;
          this.goods_spec_arr[i] = data.spec_attr[i].spec_items[0].item_id;
        }
      }
    }
    return data;
  },

  /**
   * 点击切换不同规格
   */
  modelTap: function (e) {
    let attrIdx = e.currentTarget.dataset.attrIdx,
      itemIdx = e.currentTarget.dataset.itemIdx,
      specData = this.data.specData;

    for (let i in specData.spec_attr) {
      for (let j in specData.spec_attr[i].spec_items) {
        if (attrIdx == i) {
          specData.spec_attr[i].spec_items[j].checked = false;

          if (itemIdx == j) {
            specData.spec_attr[i].spec_items[itemIdx].checked = true;
            this.goods_spec_arr[i] = specData.spec_attr[i].spec_items[itemIdx].item_id;
          }
        }
      }
    }

    this.setData({
      specData
    });
    // 更新商品规格信息
    this.updateSpecGoods(attrIdx, itemIdx);
  },

  /**
   * 更新商品规格信息
   */
  updateSpecGoods: function (attrIdx, itemIdx) {
    let skuItem = this.data.specData.spec_attr[attrIdx].spec_items[itemIdx]
    // 记录goods_sku_id
    // 更新商品价格、划线价、库存
    if (typeof skuItem === 'object') {
      this.setData({
        goods_sku_id: skuItem.id,
        goods_price: skuItem.goods_price,
        goods_sku_title: skuItem.title,
        // line_price: skuItem.form.line_price,
        stock_num: skuItem.stock_num,
      });
    }
  },

  /**
   * 设置轮播图当前指针 数字
   */
  setCurrent: function (e) {
    this.setData({
      currentIndex: e.detail.current + 1
    });
  },

  /**
   * 控制商品规格/数量的显示隐藏
   */
  onChangeShowState: function () {
    this.setData({
      showView: !this.data.showView
    });
  },

  /**
   * 返回顶部
   */
  goTop: function (t) {
    this.setData({
      scrollTop: 0
    });
  },
  /**
   * 显示/隐藏 返回顶部按钮
   */
  scroll: function (e) {
    this.setData({
      floorstatus: e.detail.scrollTop > 200
    })
  },
  /**
   * 增加商品数量
   */
  up: function () {
    this.setData({
      goods_num: ++this.data.goods_num
    })
  },

  /**
   * 减少商品数量
   */
  down: function () {
    if (this.data.goods_num > 1) {
      this.setData({
        goods_num: --this.data.goods_num
      });
    }
  },

  /**
   * 跳转购物车页面
   */
  flowCart: function () {
    wx.switchTab({
      url: "../flow/index"
    });
  },

  /**
   * 快捷导航 显示/隐藏
   */
  commonNav: function () {
    this.setData({
      nav_select: !this.data.nav_select
    });
  },

  nav: function (e) {
    let index = e.currentTarget.dataset.index;
    "home" == index ? wx.switchTab({
      url: "../index/index"
    }) : "fenlei" == index ? wx.switchTab({
      url: "../category/index"
    }) : "cart" == index ? wx.switchTab({
      url: "../flow/index"
    }) : "profile" == index && wx.switchTab({
      url: "../user/index"
    });
  },

  /**
   * 加入购物车and立即购买
   */
  submit: function (e) {
    let _this = this,
      submitType = e.currentTarget.dataset.type;

    if (submitType === 'buyNow') {
      // 立即购买
      wx.navigateTo({
        url: '../flow/checkout?' + App.urlEncode({
          order_type: 'buyNow',
          goods_id: _this.data.goods_id,
          goods_num: _this.data.goods_num,
          goods_sku_id: _this.data.goods_sku_id
        })
      });
    } else if (submitType === 'addCart') {
      // 加入购物车
      App._post_form('cart/add', {
        goods_id: _this.data.goods_id,
        goods_num: _this.data.goods_num,
        goods_sku_id: _this.data.goods_sku_id
      }, function (result) {
        if (!parseInt(_this.data.is_exist)) {
          let cart_total_num = parseInt(_this.data.cart_total_num) + 1;
          _this.setData({
            "cart_total_num": cart_total_num
          })
        }
        App.showSuccess(result.msg);
        _this.setData(result.data);
        _this.hideModal();
      });
    }
  },
  // 弹出层
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: "ease",
      delay: 0
    })
    this.animation = animation
    animation.translateY(100).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  hideModal: function () {
    this.setData({
      showModalStatus: false,
    })

  },
  flowCollect: function () {
    let _this = this;

    // 加入购物车
    App._post_form('collect/collect', {
      object_id: _this.data.goods_id,
      object_type: "goods",
    }, function (result) {
      App.showSuccess(result.msg);
      _this.setData(result.data);
      _this.hideModal();
    });
  }
})